<?php 
	defined('BASEPATH') OR exit('No direct script access allowed');
	class Welcome extends CI_Controller{
		function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->helper('lw_helper');
			$this->load->database();
			$this->load->model('Welcome_model');
		}
		public function index(){
			if($_POST){
				$username = $this->input->post('username');
				$password = $this->input->post('password');
				$code = $this->input->post('code');
				if(!$username || !$password || !$code){
					echo json_encode(array('code'=>'401','msg'=>'参数错误'));
					exit;
				}
				if(strtolower($code) == strtolower($this->session->userdata('yzm'))){
					//$this->session->unset_userdata('yzm');
				}else{
					echo json_encode(array('code'=>'401','msg'=>'验证码错误'));
					exit;
				}
				//登陆
				$result = $this->Welcome_model->login($username,$password);
				if($result){
					$this->session->set_userdata('aid',$result['id']);
					json_return(200,'ok');
				}else{
					echo json_encode(array('code'=>'401','msg'=>'用户名或密码错误'));
					exit;
				}

			}else{
				
				$data = self::yzm1();
				$this->load->vars('imgurl',$data);

				$this->load->view('welcome/login');
				$this->load->view('public/header');
			}
		}
		public function yzm(){
			$this->load->helper('captcha');
			$vals = array(
			    'word'      => '',
			    'img_path'  => './captcha/',
			    'img_url'   => 'http://127.0.0.1/captcha/',
			    'font_path' => './path/oneshadow.otf',
			    'img_width' => '130',
			    'img_height'    => 46,
			    'expiration'    => 7200,
			    'word_length'   => 4,
			    'font_size' => 16,
			    'img_id'    => 'Imageid',
			    'pool'      => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',

			    // White background and border, black text and red grid
			    'colors'    => array(
			        'background' => array(255, 255, 255),
			        'border' => array(255, 255, 255),
			        'text' => array(0, 0, 0),
			        'grid' => array(255, 40, 40)
			    )
			);

			$cap = create_captcha($vals);
			$this->session->set_userdata('yzm',$cap['word']);
			echo '/captcha/'.$cap['filename'];
			//return  '/captcha/'.$cap['filename'];
			//return  $cap['image'];
		}
		public function yzm1(){
			$this->load->helper('captcha');
			$vals = array(
			    'word'      => '',
			    'img_path'  => './captcha/',
			    'img_url'   => 'http://127.0.0.1/captcha/',
			    'font_path' => './path/oneshadow.otf',
			    'img_width' => '130',
			    'img_height'    => 46,
			    'expiration'    => 7200,
			    'word_length'   => 4,
			    'font_size' => 16,
			    'img_id'    => 'Imageid',
			    'pool'      => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',

			    // White background and border, black text and red grid
			    'colors'    => array(
			        'background' => array(255, 255, 255),
			        'border' => array(255, 255, 255),
			        'text' => array(0, 0, 0),
			        'grid' => array(255, 40, 40)
			    )
			);

			$cap = create_captcha($vals);
			$this->session->set_userdata('yzm',$cap['word']);
			//echo '/captcha/'.$cap['filename'];
			return  '/captcha/'.$cap['filename'];
			//return  $cap['image'];
		}
	}